package com.example.highclasscomponent.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.highclasscomponent.R;
import com.example.highclasscomponent.pojo.BillInfo;

import java.util.List;

public class BillListAdapter extends BaseAdapter {
    Context context;
    List<BillInfo> mBillInfoList;

    public BillListAdapter(Context context, List<BillInfo> billInfoList) {
        this.context = context;
        this.mBillInfoList = billInfoList;
    }

    @Override
    public int getCount() {
        return mBillInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        return mBillInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return mBillInfoList.get(position).id;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null)
        {
            viewHolder=new ViewHolder();
            convertView= LayoutInflater.from(context).inflate(R.layout.item_bill,null);
            viewHolder.tv_date=convertView.findViewById(R.id.tv_date);
            viewHolder.tv_remark=convertView.findViewById(R.id.tv_remark);
            viewHolder.tv_amount=convertView.findViewById(R.id.tv_amount);
            convertView.setTag(viewHolder);
        }
        else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        BillInfo info=mBillInfoList.get(position);
        viewHolder.tv_date.setText(info.date);
        viewHolder.tv_remark.setText(info.remark);
        viewHolder.tv_amount.setText(String.format("%s%d元",info.type==0?"+":"-",(int)info.amount));
        return convertView;
    }
    class ViewHolder
    {
        TextView tv_date;
        TextView tv_remark;
        TextView tv_amount;
    }
}
